package com.whh.controller;

import javax.servlet.http.HttpServletRequest;

import com.whh.config.vo.ResultCode;
import com.whh.config.vo.ResultVO;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import com.whh.entity.BaseEntity;
import com.whh.entity.ResultEntity;
import com.whh.service.BaseService;

import java.util.List;

@CrossOrigin
public class BaseController<T extends BaseEntity> {
	
	private BaseService<T> service;

	public BaseService<T> getService() {
		return service;
	}

	public void setService(BaseService<T> service) {
		this.service = service;
	}
	
	@RequestMapping(value="/get",method=RequestMethod.GET)
	public ResultVO<T> get(T entity) {
		return new ResultVO<>(ResultCode.SUCCESS,service.getById(entity.getId()));
	}
	
	@RequestMapping(value="/search",method=RequestMethod.GET)
	public ResultVO<List<T>> search(T entity) {
		return new ResultVO<>(ResultCode.SUCCESS,service.search(entity));
	}
	
	@RequestMapping(value="/searchByPage",method=RequestMethod.GET)
	public ResultVO<ResultEntity> searchByPage(HttpServletRequest request,T entity,Integer page, Integer limit) {
		return new ResultVO<>(ResultCode.SUCCESS,service.searchByPage(entity,page,limit));
	}
	
	@RequestMapping(value="/add",method=RequestMethod.POST)
	public ResultVO<T> add(T entity) {
		return new ResultVO<>(ResultCode.SUCCESS,service.add(entity));
	}
	
	@RequestMapping(value="/update",method=RequestMethod.POST)
	public ResultVO<T> edit(T entity) {
		return new ResultVO<>(ResultCode.SUCCESS,service.update(entity));
	}
	
	@RequestMapping(value="/del",method=RequestMethod.POST)
	public ResultVO<Object> del(T entity) {
		return new ResultVO<>(ResultCode.SUCCESS,service.del(entity));
	}
	
	@RequestMapping(value="/sum",method=RequestMethod.GET)
	public ResultVO<List<T>> sum(T entity) {
		return new ResultVO<>(ResultCode.SUCCESS,service.sum(entity));
	}
	
	@RequestMapping(value="/total",method=RequestMethod.GET)
	public ResultVO<List<T>> total(T entity) {
		return new ResultVO<>(ResultCode.SUCCESS,service.total(entity));
	}

	@RequestMapping(value="/init",method=RequestMethod.GET)
	public ResultVO<T> init(T entity) {
		return new ResultVO<>(ResultCode.SUCCESS,service.init(entity));
	}
}
